﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class UsersController : BaseController<user>
    {
        //
        // GET: /Users/
        protected admin CurrentUer
        {
            get { return Session["CurrentUer"] == null ? null : (admin)Session["CurrentUer"]; }
        }

        public override BLL.BaseBll<user> bll
        {
            get
            {
                return new UserBll();
            }
        }

        public class ListUser
        {
            public int userCode;  //userCode  userName userPhone userSex wechatCode wechatName userMemberstatus userState  memberExp  memberBalance memberCoupon
            public string userName;
            public string userPhone;
            public string userSex;
            public string wechatCode;
            public string wechatName;
            public string userMemberstatus;
            public string userState;
            public int memberExp; //积分memberExp  
            public double memberBalance; //余额memberBalance

        }
        List<ListUser> listUser = new List<ListUser>();

        MemberBll memberBll = new MemberBll();

        //查询用户信息
        public ActionResult GetUsers(int page, int rows, user model)
        {
            Expression<Func<user, bool>> condition = x => true;
            int count;
            // 1.先查询出所有的用户信息    
            var list = bll.Search(model, x => true, true, x => x.userCode, rows, page, out count).Where(condition).Select(x => new { x.userCode, x.userName, x.userPhone, x.userSex, x.wechatCode, x.wechatName, x.userMemberstatus, x.userState }).ToList();
            foreach (var oneCode in list)
            {   
                //在查询该用户对应的积分余额
                var listMember = memberBll.Search(x => x.wechatCode == oneCode.wechatCode).Select(x => new { x.wechatCode, x.memberExp, couponId = x.Id, x.memberBalance }).ToList();
                if (listMember.Count == 0)
                {
                    ListUser salePro = new ListUser();
                    salePro.userCode = oneCode.userCode;
                    salePro.userName = oneCode.userName;
                    salePro.userPhone = oneCode.userPhone;
                    salePro.userSex = oneCode.userSex == 1 ? "男" : oneCode.userSex == 2 ? "女" : "未知";
                    salePro.wechatCode = oneCode.wechatCode;
                    salePro.wechatName = oneCode.wechatName;
                    salePro.userState = oneCode.userState == 1 ? "有效" : "禁用";//0，待审核，1，审核通过未通知，2，审核通过已通知，3，审核未通过未通知，4，审核未通过已通知
                    salePro.userMemberstatus = oneCode.userMemberstatus == 0 ? "待审核" :"审核通过";
                    listUser.Add(salePro);
                }
                if (listMember.Count != 0)
                {
                    ListUser salePro = new ListUser();
                    salePro.userCode = oneCode.userCode;
                    salePro.userName = oneCode.userName;
                    salePro.userPhone = oneCode.userPhone;
                    salePro.userSex = oneCode.userSex == 1 ? "男" : oneCode.userSex == 2 ? "女" : "未知";
                    salePro.wechatCode = oneCode.wechatCode;
                    salePro.wechatName = oneCode.wechatName;
                    salePro.userState = oneCode.userState == 1 ? "有效" : "禁用";
                    salePro.userMemberstatus = oneCode.userMemberstatus == 0 ? "待审核" : oneCode.userMemberstatus == 1 || oneCode.userMemberstatus == 2 ? "审核通过" : "审核未通过已通知";
                    salePro.memberExp = listMember.First().memberExp; 
                    salePro.memberBalance = Convert.ToDouble(listMember.FirstOrDefault().memberBalance);
                    listUser.Add(salePro);
                }
            }
            return Json(new { rows = listUser, total = count });
        }

        //禁用用户  修改状态
        public ActionResult ModifyState(int id)
        {
            string msg = "禁用用户失败";
            string state = "false";
            user model = new user();
            var list = bll.Search(x => x.userCode == id).ToList();
            model = list.FirstOrDefault();
            model.userState = 0;
            if (bll.Modify(model) > 0)
            {
                msg = "禁用成功";
                state = "true";
            }
            return Json(new { state = state, msg = msg });
        }

        /// <summary>
        /// 获取今日新增用户 todaySum allSum     faceNum 人脸通过人数
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAllOrToday()
        {
            var listAll = bll.Search(x=>x.userState==1).Select(x => new { x.wechatCode }).ToList();
            var faceList= bll.Search(x => x.userState == 1&&x.userMemberstatus==2).Select(x => new { x.wechatCode }).ToList();
            DateTime todayNow = DateTime.Now;
            DateTime midnight = DateTime.Now;
            midnight = midnight.Date;   
            var listToday = bll.Search(x => x.userState == 1 && x.registerTime <= todayNow && x.registerTime >= midnight).Select(x => new { x.wechatCode }).ToList();
            return Json(new { todaySum = listToday.Count, allSum = listAll.Count ,faceNum=faceList.Count()}, JsonRequestBehavior.AllowGet);
               
        }
    }
}
